import {
  CourseTag,
  ListCourseWithPrice,
  ListCourse,
  Course,
  CourseCatalog,
  CourseItem,
  CourseVideo,
  CourseArticle,
  CourseLiveStatus,
  StudyRecord,
  CourseItemDetail,
} from "@/types/course";
import request from "@/utils/request";

const baseURL = "/train/course";

/**
 * 获取课程标签列表
 */
export const getCourseTagList = async () => {
  const res = await request<Res<CourseTag[]>>({
    url: `${baseURL}/list-tag`,
    method: "GET",
  });
  return res;
};

/**
 * 分页获取课程列表
 *
 * 过程：在product_spu表中获取全部spu_type=2的商品的id，再通过这些id去product_sku中spu_id=product_spu.id的sku，
 * 再通过sku_id去sku_bind表里面查sku_id=sku.id的bind_id，再通过bind_id去course表里面查course.id=bind_id的课程
 */
export const getCoursePage = async (params: PageParams) => {
  const res = await request<Res<{ list: ListCourseWithPrice[]; total: number }>>({
    url: `${baseURL}/get-course-page`,
    method: "POST",
    data: params,
  });

  return res;
};

/**
 * 获取已购买的课程列表
 *
 * 过程：在member_course表中获取用户绑定的课程id，然后联表查询course表，获取所有的course信息
 */
export const getPurchasedCoursePage = async (params: PageParams) => {
  const res = await request<Res<{ list: ListCourse[]; total: number }>>({
    url: `${baseURL}/get-purchased-course-page`,
    method: "Post",
    data: params,
  });
  return res;
};

/**
 * 根据课程id获取课程
 * @param id - 课程id
 */
export const getCourseById = async (id: string) => {
  const res = await request<Res<Course>>({
    url: `${baseURL}/get-course-by-id`,
    method: "GET",
    params: { id },
  });
  return res;
};

/**
 * 根据课程id获取课程目录
 * @param courseId - 课程id
 */
export const getCourseCatalogListByCourseId = async (courseId: string) => {
  const res = await request<Res<CourseCatalog[]>>({
    url: `${baseURL}/get-catalog-list-by-course-id`,
    method: "GET",
    params: { courseId },
  });
  return res;
};

/**
 * 根据课程id获取课程条目
 * 接口更改：
 *  如果是条目直播：
 *      1、需要获取对应直播的开始和结束时间，直播名称
 *  如果是视频：
 *      1、需要去 train_course_study_record获取对应视频的study_progress字段返回
 *      2、也需要返回课程视频的时长字段video_duration
 * @param id - 课程id
 */
export const getCourseItemListByCourseId = async (courseId: string) => {
  const res = await request<Res<CourseItem[]>>({
    url: `${baseURL}/get-item-list-by-course-id`,
    method: "GET",
    params: { courseId },
  });
  return res;
};

/**
 * 根据视频id获取课程视频
 * @param id - 视频id
 */
export const getCourseVideoById = async (id: string) => {
  const res = await request<Res<CourseVideo>>({
    url: `${baseURL}/get-video-by-id`,
    method: "GET",
    params: { id },
  });
  return res;
};

/**
 * 根据根据文章id获取课程文章
 * @param id - 文章id
 */
export const getCourseArticleById = async (id: string) => {
  const res = await request<Res<CourseArticle>>({
    url: `${baseURL}/get-article-list-by-id`,
    method: "GET",
    params: { id },
  });
  return res;
};

export const getCourseLive = async (liveId: string) => {
  const res = await request<Res<CourseLiveStatus>>({
    url: `${baseURL}/get-live-info-by-live-id`,
    method: "GET",
    params: { liveId },
  });
  return res;
};

/** 获取目录下全部item的详情 */
export const listItem = async (catalogId: CourseCatalog["id"]) =>
  request<Res<CourseItemDetail[]>>({
    url: `${baseURL}/list-item`,
    method: "POST",
    params: { courseCatalogId: catalogId },
  });

export const getPlayerTokenByBjyVideoId = async (videoId: CourseVideo["id"]) => {
  const res = await request<Res<string>>({
    url: `${baseURL}/get-player-token-by-video-id`,
    method: "GET",
    params: { videoId },
  });
  return res;
};

export const updateStudyRecord = async (
  data: {
    courseId: string;
    itemId: string;
  } & StudyRecord
) => {
  const res = await request<Res<null>>({
    url: `${baseURL}/update-study-record`,
    method: "POST",
    data,
  });
  return res;
};
